package Enea.server;

import Enea.project.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * Classe incaricata di leggere i messaggi WUTP provenienti dal client.
 * @author davide
 */
public abstract class WUTPServerReader {
    
    public static ClientData readMessage1x(Document message) {
        NodeList temp;
        long cpuclock;
        int cpunumber;
        
        temp = message.getElementsByTagName("cpuclock");
        
        if (temp.getLength()!=1) cpuclock = 0;
        else cpuclock = Long.parseLong(temp.item(0).getTextContent());
        
        temp = message.getElementsByTagName("cpunumber");
        
        if (temp.getLength()!=1) cpunumber = 0;
        else cpunumber = Integer.parseInt(temp.item(0).getTextContent());
        
        return new ClientData(cpuclock, cpunumber);
    }
    
    public static int readMessage2x(Document message) {
        NodeList temp;
        int clientid;
        
        temp = message.getElementsByTagName("clientid");
        
        if (temp.getLength()!=1) clientid = -1;
        else clientid = Integer.parseInt(temp.item(0).getTextContent());
        
        return clientid;
    }
    
    //TODO: Deve lanciare eccezione.
    public static ResultsPack readMessage3x(Document message) {
        NodeList temp;
        long sizebyte = 0;
        long sizepu = 0;
        String data = null;
               
        temp = message.getElementsByTagName("sizebyte");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else sizebyte = Long.parseLong(temp.item(0).getTextContent());
        
        temp = message.getElementsByTagName("sizepu");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else sizepu = Long.parseLong(temp.item(0).getTextContent());
             
        temp = message.getElementsByTagName("data");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else data = temp.item(0).getTextContent();
        
        return new ResultsPack(sizebyte, sizepu, data);
    }
    
    public static ClientStats readMessage4x(Document message) {
        NodeList temp;
        long elapsedbyte = 0;
        long elapsedpu = 0;
        float progress = 0;
        float speedbyte = 0;
        float speedpu = 0;
               
        temp = message.getElementsByTagName("elapsedbyte");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else elapsedbyte = Long.parseLong(temp.item(0).getTextContent());
        
        temp = message.getElementsByTagName("elapsedpu");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else elapsedpu = Long.parseLong(temp.item(0).getTextContent());
             
        temp = message.getElementsByTagName("progress");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else progress = Float.parseFloat(temp.item(0).getTextContent());
        
        temp = message.getElementsByTagName("speedbyte");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else speedbyte = Float.parseFloat(temp.item(0).getTextContent());
        
        temp = message.getElementsByTagName("speedpu");
        
        if (temp.getLength()!=1) ;//EXCEPTION;
        else speedpu = Float.parseFloat(temp.item(0).getTextContent());
        
        return new ClientStats(progress, elapsedbyte, elapsedpu, speedbyte, speedpu);
    }
    
}
